package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.common.constant.BalanceLogChangeTypeContent;
import com.tbit.common.constant.PayPlatformContent;
import com.tbit.common.constant.TableNameConstant;
import com.tbit.common.entity.BalanceChange;
import com.tbit.common.entity.UserCapitalChange;
import com.tbit.uqbike.client.dao.core.GiftCardUserDao;
import com.tbit.uqbike.client.pojo.GiftCardUser;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserToAgent;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:30 2020/1/3
 */
@Service("giftCardUserService")
public class GiftCardUserServiceImpl implements GiftCardUserService {
    @Autowired
    private GiftCardUserDao giftCardUserDao;
    @Autowired
    private PushMsgLogService pushMsgLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserToAgentService userToAgentService;
    @Autowired
    private UserAccountMoneyService userAccountMoneyService;

    @Override
    public GiftCardUser getByUserId(Integer userId, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("accountId", accountId);
        return giftCardUserDao.getByUserId(map);
    }

    @Override
    public void insert(GiftCardUser giftCardUser, String remark, Integer businessType, String orderNO, String dateTime) {
        GiftCardUser giftCardUserDB = getByUserId(giftCardUser.getUserId(), giftCardUser.getAccountId());
        //String dateTime =DateTimeUtil.getNowTime();
        giftCardUser.setUpdateTime(dateTime);
        Long changeStartMoney;
        Long changeEndMoney;
        Long changeMoney = Math.abs(giftCardUser.getMoney().longValue());
        if (giftCardUserDB == null) {
            giftCardUserDao.insert(giftCardUser);
            changeStartMoney = 0L;
            changeEndMoney = giftCardUser.getMoney().longValue();
        } else {
            giftCardUserDao.edit(giftCardUser);
            changeStartMoney = giftCardUserDB.getMoney().longValue();
            changeEndMoney = giftCardUserDB.getMoney().longValue() + giftCardUser.getMoney().longValue();
        }
        Integer changeType = giftCardUser.getMoney() < 0 ? PayPlatformContent.PAY_USE : PayPlatformContent.PAY_RECHARGE;
        Integer capitalType = BalanceLogChangeTypeContent.capitalType_giftCard;
        /**获取用户区域绑定元区域id*/
        UserToAgent userToAgent = userToAgentService.getByAccountId(giftCardUser.getUserId());
        Integer originalAccountId = giftCardUser.getAccountId();
        if (userToAgent != null) {
            originalAccountId = userToAgent.getFirstId();
        }
        User user = userService.getByUserId(giftCardUser.getUserId());
        /**日志入库*/
        if (PayPlatformContent.PAY_RECHARGE.equals(changeType)) {
            UserCapitalChange userCapitalChange = new UserCapitalChange(user.getUserId(), user.getPhone(), user.getName()
                    , giftCardUser.getAccountId(),
                    user.getAccountId(), changeStartMoney, changeMoney, changeEndMoney, changeType, capitalType,
                    businessType, orderNO, null, null, null
                    , orderNO, orderNO, DateTimeUtil.StringToDateTime(dateTime).getTime(),
                    remark, originalAccountId);
            pushMsgLogService.sendPushLog(JSON.toJSONString(userCapitalChange), TableNameConstant.USERCAPITALCHANGE);
            BalanceChange balanceChange = new BalanceChange(user.getUserId(), user.getPhone(), user.getName(), giftCardUser.getAccountId()
                    , user.getAccountId(), changeStartMoney, changeMoney
                    , changeEndMoney, changeType
                    , capitalType, businessType
                    , DateTimeUtil.StringToDateTime(dateTime).getTime(), remark, orderNO);
            pushMsgLogService.sendPushLog(JSON.toJSONString(balanceChange), TableNameConstant.BALANCECHANGE);
        }
    /*BalanceChange balanceChange = new BalanceChange(user.getUserId(),user.getPhone(),user.getName(),giftCardUser.getAccountId()
        ,user.getAccountId(),changeStartMoney,changeMoney
        ,changeEndMoney, changeType
        ,capitalType,businessType
        ,DateTimeUtil.StringToDateTime(dateTime).getTime(),remark,orderNO);
    pushMsgLogService.sendPushLog(JSON.toJSONString(balanceChange), TableNameConstant.BALANCECHANGE);*/
        /**用户区域余额记录*/
        userAccountMoneyService.editMoney(user, orderNO, changeMoney, capitalType, giftCardUser.getAccountId()
                , changeType, DateTimeUtil.StringToDateTime(dateTime).getTime(), remark, businessType);
    }


    @Override
    public List<GiftCardUser> getListByUserId(Integer userId) {

        return giftCardUserDao.getListByUserId(userId);
    }
}
